import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Random;


/**
 * <code>ImageCatalog</code> class stores <code>ProfileImages</code> that represent user's
 * friends' profile images. The images are stored in an <code>ArrayList</code> sorted by 
 * color of the image. <code>ImageCatalog</code>'s main method is <code>getBestImage</code> 
 * - it is the key method in creating a friend matrix.
 * 
 */
public class ImageCatalog {

	/**The <code>ArrayList</code> that stores the images used to create friend matrix.*/
	private ArrayList<ProfileImage> images;
	
	/**
	 * The constructor takes no parameters, it only initializes the <code>images 
	 * ArrayList</code>.
	 */
	public ImageCatalog() {
		this.images = new ArrayList<ProfileImage>();
	}
	
	/**
	 * Adds given <code>image</code> to the <code>images</code>.
	 * 
	 * @param image	the <code>ProfileImage</code> that represent a friend
	 */
	public void addImage(ProfileImage image){
		this.images.add(image);
	}
	
	/**
	 * Sorts <code>ProfileImage</code>s stored in the <code>images ArrayList</code>
	 */
	public void sortImages() {
		Collections.sort(this.images, new ColorComparator());
	}
	
	/**
	 * The method goes through the <code>ImageCatalog</code> returning an image that
	 * represents given <code>color</code> the best.
	 * 
	 * @param color	the color that needs to be replaced by an image of a friend
	 * @return		the best image that represents the specified color
	 */
	public ProfileImage getBestImage(int color){
		
		ProfileImage best = null;
		int bestDist = Integer.MAX_VALUE;
		
		for(int i = 0; i < this.images.size(); i++) {
			
			ProfileImage img = this.images.get(i);
			int clr = img.getColor();
			int dist =  Math.abs(clr - color);
		
			if (clr == 0){
				//System.out.println("null image");
			} else if ( dist < bestDist) {
				best = img;
				bestDist = dist;
			} else {
				break;
			}
		}
		
		if (best == null){
			best = this.getRandomImage();
		}
			
		return best;
	}
	
	/**
	 * If everything else fails this method is used to retrieve random picture from
	 * <code>ImageCatalog</code>.
	 * 
	 * @return	random image from the <code>ImageCatalog</code>
	 */
	private ProfileImage getRandomImage(){
		Random generator = new Random();
		int rnd = generator.nextInt(this.images.size());
		
		ProfileImage user = this.images.get(rnd);
		while (user.getPic_square() == null){
			this.images.remove(user);
			user = this.images.get(rnd);
		}
		return user;
	}
	
}

/**
 * The class is used to sort the <code>ArrayList</code> in <code>ImageCatalog</code> using
 * friends' profile images' color value as criteria. Sorting makes it faster to go through 
 * the <code>ImageCatalog</code> and find the image of right color.
 *
 */
class ColorComparator implements Comparator<ProfileImage> {

	/**
	 * Compares the two given <code>ProfileImage</code>s and returns integer 
	 * according to the color values of the two images. A negative integer is returned
	 * in case that the <code>p1</code> has greater color value, zero if the two are equal 
	 * and a positive value if <code>p2</code> has greater color value.
	 * 
	 * @param p1	first <code>ProfileImage</code> to be compared
	 * @param p2	second <code>ProfileImage</code> to be compared
	 * @return		a negative integer, zero, or a positive integer according to 
	 * 				<code>p1</code>'s and <code>p2</code>'s color values.
	 */
	public int compare(ProfileImage p1, ProfileImage p2) {
		
		int c1 = p1.getColor();
		int c2 = p2.getColor();

		if (c1 > c2){
			return -1;
		} else if (c2 > c1) {
			return 1;
		} else {
			return 0;
		}
	}
	
}
